package com.gdcvi.scan;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.Socket;

/**
 * 游戏客户端
 */
public class GameClient implements HandConnection {
    private Socket socket;
    private int port;
    public GameClient(int port) throws Exception{
        socket=new Socket("127.0.0.1",port);
    }

    @Override
    public void send(Hand hand) {
        try {
            Field field =  hand.getClass().getField("编号");
            int num = field.getInt(hand);
            socket.getOutputStream().write(num);
        }catch (Exception e){
            throw new RuntimeException( e.getMessage());
        }
    }

    @Override
    public Hand income() {
        try {
            InputStream input =  socket.getInputStream();
            int hand = input.read();
            return  new PalyFactory().createHand(hand);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public void close() {
        try {
            socket.close();
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
}
